package cc.comac.saic.hkhy.jsrj.gj.inttools.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.saic.hkhy.jsrj.gj.inttools.constant.FileType;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.entity.FileInfo;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.entity.FileVersionInfo;
import cc.comac.saic.hkhy.jsrj.gj.inttools.service.FileInfoService;
import cc.comac.saic.hkhy.jsrj.gj.inttools.service.FileVersionInfoService;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 文件下载
 *
 * @author shisongyan
 * @date 2025/7/10
 */

@RestController
@RequestMapping("file-download")
public class FileDownloadController {
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private FileVersionInfoService fileVersionInfoService;


    /**
     * 8.1.6	高层需求模型获取接口
     */
    @GetMapping("{projKey}/hlr-model")
    public void hlrModelDownload(HttpServletResponse response,
                                 @PathVariable String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.HLR_MODEL.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName.toString());
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 8.1.12	控制模型生成代码获取接口
     */
    @GetMapping("{projKey}/control-code")
    public void controlCodeDownload(HttpServletResponse response,
                                    @PathVariable String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.CONTROL_CODE.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName.toString());
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 8.1.13	显示模型生成代码获取接口
     */
    @GetMapping("{projKey}/display-code")
    public void displayCodeDownload(HttpServletResponse response,
                                    @PathVariable String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.DISPLAY_CODE.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName.toString());
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 8.1.15	工程代码获取接口
     */
    @GetMapping("{projKey}/impl-code")
    public void implCodeDownload(HttpServletResponse response,
                                 @PathVariable String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.IMPL_CODE.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName.toString());
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 根据file version id下载文件
     */
    @GetMapping("/file-version/{fileVersionId}")
    public void fileVersionDownload(HttpServletResponse response,
                                    @PathVariable Long fileVersionId) {

        FileVersionInfo fileVersionInfo = fileVersionInfoService.queryById(fileVersionId);
        FileInfo fileInfo = fileInfoService.queryById(fileVersionInfo.getFileInfoId());
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.downloadByFileVersionId(fileVersionId);

            // 设置响应头
            Path fileName = Paths.get(fileInfo.getFilePath()).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName.toString());
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
